<template>
    <view class="gift-purchase">
    <view class="header">
      <view><uni-status-bar></uni-status-bar></view>

      <view class="search-box flex-cn">
        <view class="navbar-left flex-cn" style="flex: 1" @click="backHome">
          <view
            style="width: 100rpx;color: #fff; text-align: center"
            class="qh-rt-single qh-rt-zuo-zuo navbar-back-icon"
          >
          </view>
        </view>

        <view
          style="
            height: 80rpx;
            line-height: 80rpx;
            font-size: 36rpx;
            font-family: '.PingFang SC, PingFang SC';
            display: flex;
            justify-content: center;
            align-content: center;
            flex: 1;
            color: #fff;
            font-weight: 600;
          "
        >
          {{ title }}
        </view>
        <view style="width: 320rpx; flex: 1"></view>
      </view>
    </view>


    <view class=" shuom-benner_footer" style="height: 100vh;">
      <view class="container">
        <!-- Main Card -->
        <view class="card">
          <!-- Header -->
          <!-- <view class="card-header">
            <text class="header-title">【团购洽谈】</text>
          </view> -->

          <!-- Content -->
          <view class="card-content">
            <text class="greeting">尊敬的新老会员：</text>
            <text class="message"
              >
              请您添加我们的专属客服微信号，以便我们向您发放专属精选商品的优惠券。此类优惠券为该渠道每月定期发放，不对外公开。感谢您的理解与配合。
              
              </text
            >

            <view class="vip_user_container">
              <view class="vip_user_product-grid">
                <view class="vip_user_product-item" v-for="(item, index) in updatedProductList" :key="index">
                  <view class="vip_user_product-content">
                    <text class="vip_user_product-name">{{item.goods_name}}</text>
                    <image :src="item.goods_image" mode="aspectFit" class="vip_user_product-image"></image>
                    <view class="vip_user_product-info">
                   
                      <view class="vip_user_price-info">
                        <view class="vip_user_original-price">
                          <text class="vip_user_label1">零售价：</text>
                          <text style="font-weight: 600;">¥{{item.online_price}}</text>
                        </view>
                        <view class="vip_user_discount-price">
                          <text class="vip_user_label">券后价：</text>
                          <text>¥{{item.discountedPrice}}</text>
                        </view>
                      </view>
                    </view>
                  </view>
                </view>
              </view>
            </view>

            <view class="qr-section">
              <view class="qr-codes-container">
                <view class="qr-code-item">
                  <image
                    class="qr-code"
                       src="https://safood-1317438654.cos.ap-guangzhou.myqcloud.com/tempImage/1735198747676d081baa124.png"
                    mode="aspectFit"
                    show-menu-by-longpress="true"
                  ></image>
                  <text class="qr-text">客服专员：福美</text>
                </view>
                <!-- <view class="qr-code-item">
                  <image
                    class="qr-code"
                    src="https://safood-1317438654.cos.ap-guangzhou.myqcloud.com/tempImage/1735198771676d083354b28.png"
                    mode="aspectFit"
                    show-menu-by-longpress="true"
                  ></image>
                  <text class="qr-text">客服专员：福珊</text>
                </view> -->
              </view>

              
              <view style="display: flex; justify-content: center;font-weight: 600;margin-bottom: 20rpx;">长按二维码添加</view>

            </view>


          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      title: "添加客服",
      isTop: true,
      isScrolled: false,
      productList: [
        {
          name: '正官庄红参元 1L100ML*10瓶',
          image: 'https://safood-1317438654.cos.ap-guangzhou.myqcloud.com/Uploads/UEditor/173563067067739f4eecb54.jpg',
          originalPrice: '69.9',
          discountPrice: '39.9'
        },
  
      ],
      couponList:[]
    };
  },

  onLoad() {
    this.getList()
  },
  computed: {
  updatedProductList() {
    // 如果productList为空，返回空数组
    if (!this.productList || this.productList.length === 0) {
      return [];
    }

    return this.productList.map(product => {
      // 如果couponList为空，直接返回原始商品信息
      if (!this.couponList || this.couponList.length === 0) {
        return {
          ...product,
          discountedPrice: product.online_price // 使用原价作为折后价
        };
      }

      // 查找对应的优惠券
      const coupon = this.couponList.find(coupon => coupon.goods_id === product.id);

      // 确保商品的价格是有效的
      const onlinePrice = parseFloat(product.online_price);
      if (isNaN(onlinePrice) || onlinePrice <= 0) {
        // 如果商品价格无效，直接返回原始商品信息
        return product;
      }

      // 如果找到了匹配的优惠券，计算折后价
      if (coupon) {
        // 确保优惠券的减少金额是有效的
        const reduceMoney = parseFloat(coupon.reducemoney);
        if (isNaN(reduceMoney) || reduceMoney <= 0) {
          // 如果优惠券的减少金额无效，直接返回原始商品信息
          return {
            ...product,
            discountedPrice: product.online_price // 使用原价作为折后价
          };
        }

        // 计算折后价并保留两位小数
        const discountedPrice = Math.max(0, onlinePrice - reduceMoney).toFixed(2);
        return {
          ...product,
          discountedPrice,  // 计算后的折后价
        };
      } else {
        // 如果没有优惠券，返回原始商品信息，但添加discountedPrice
        return {
          ...product,
          discountedPrice: product.online_price // 使用原价作为折后价
        };
      }
    });
  },
},

  methods: {

    async getList(){
      const productRes = await this.$request(
        "SmallProgramApi/Goods/getGoodsList_v2",
        { 
          store_id: this.$store.mallStoreId,
          id: 895,  
          type: 1, page: 1, limit:10,        
        }   
      );
      
      // 确保productRes有data属性并且是数组
      if (productRes && productRes.data) {
        this.productList = productRes.data;
        console.log('Product List:', this.productList);
      }

      // 获取优惠券列表的请求
      const couponRes = await this.$request(
        "SmallProgramApi/ShiFu/getVipCouponList",
        {
          store_id: this.$store.mallStoreId,
        }
      );
      
      // 确保couponRes有data属性并且是数组
      if (couponRes && couponRes.data) {
        this.couponList = couponRes.data;
        console.log('Coupon List:', this.couponList);
      }

      console.log('Product List Length:', this.productList.length);
      console.log('Coupon List Length:', this.couponList.length);
    },


    backHome() {
      uni.navigateBack();
    },
    makePhoneCall() {
      uni.makePhoneCall({
        phoneNumber: '15959296192', // Replace with actual phone number
        fail(err) {
          console.error('Phone call failed:', err)
        }
      })
    },
    openLocation() {
      // Replace with actual coordinates
      uni.openLocation({
        latitude: 24.4800,
        longitude: 118.1000,
        name: '君临宝邸商场',
        address: '厦禾路689号君临宝邸商场1层东南角',
        fail(err) {
          console.error('Opening location failed:', err)
        }
      })
    },
    handleQrTap() {
      console.log('二维码被点击');
      uni.showToast({
        title: '长按可以识别二维码',
        icon: 'none'
      });
    },
  },
};
</script>


<style>
.vip_user_container {
  padding: 10rpx;
  background-color: #E8E3CB;
  border-radius: 20rpx;
}

.vip_user_product-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* 两列布局 */
  gap: 20rpx; /* 间距 */
  padding: 20rpx;
}

.vip_user_product-content {
  background-color: #ffffff;
  border-radius: 20rpx;
  padding: 20rpx;
  height: 100%;
  box-sizing: border-box;
  display: grid;
  grid-template-rows: auto 1fr; /* 图片自适应高度，商品信息占剩余空间 */
  gap: 10rpx;
}

.vip_user_product-image {
  width: 100%;
  height: 160rpx;
  object-fit: cover;
}

.vip_user_product-info {
  display: grid;
  grid-template-rows: auto 1fr; /* 商品名称自适应高度，价格信息占剩余空间 */
  gap: 15rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center
}

.vip_user_product-name {
  font-size: 24rpx;
  font-weight: 600;
  color:#DA5C47;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2 ;
  overflow: hidden;
  line-height: 1.4;
  text-align: center;
}

.vip_user_price-info {
  font-size: 24rpx;
  display: grid;
  gap: 6rpx;
}

.vip_user_original-price {
  color: #999;
}

.vip_user_original-price text {
  text-decoration: line-through;
}

.vip_user_discount-price {
  color: #fff;
  background-color: #DA5C47;
 /* width: 100%; */
 padding: 10rpx;
 border-radius: 32rpx;

}

.vip_user_label {
  color: #fff;
  display: inline-block;
  width: 100rpx;
}
.vip_user_label1 {
  color: #767676;
  display: inline-block;
  width: 100rpx;
}

.vip_user_price-info .vip_user_original-price text:last-child,
.vip_user_price-info .vip_user_discount-price text:last-child {
  /* font-weight: bold; */
}

/* 响应式布局 */
@media screen and (min-width: 768rpx) {
  .vip_user_product-grid {
    grid-template-columns: repeat(3, 1fr); /* 在较大屏幕上显示三列 */
  }
}

@media screen and (min-width: 1024rpx) {
  .vip_user_product-grid {
    grid-template-columns: repeat(4, 1fr); /* 在更大屏幕上显示四列 */
  }
}
</style>


<style lang="scss" scoped>

.offline-center{
    overflow-y: auto;
    padding-bottom: env(safe-area-inset-bottom);
}

.gift-purchase {
  height: 100vh;  
  overflow-y: auto;  
  width: 100%;
  padding-bottom: 24rpx;
}

.header{
    background-color: #fff;
    position: sticky;
    top: 0px;
    z-index: 99;
    background-color: #f5f5f5;
  background-image: url('https://safood-1317438654.cos.ap-guangzhou.myqcloud.com/tempImage/173494563867692b66b028b.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
</style>

<style >
.action-text{
  font-size: 24rpx;
  color: #000;
  padding: 10rpx 20rpx;
  background-color: #EEEEEE;
  border-radius: 32rpx;
  text-align: center;
  width: 100rpx;
}

.container {
  padding: 0;
  width: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}

.card {
  background-color: transparent;
  border-radius: 20rpx;
  padding: 50rpx; 
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  background-image: url('https://safood-1317438654.cos.ap-guangzhou.myqcloud.com/tempImage/1735006999676a1b17eb2d9.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  display: flex;
  flex-direction: column;
  margin: 20rpx;
}

.card-header {
  margin-bottom: 30rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #DA5C47;
}

.header-title {
  font-size: 36rpx;
  color: #333;
  font-weight: bold;
}

.greeting {
  font-size: 32rpx;
  color: #333;
  /* margin-bottom: 20rpx; */
  display: block;
  font-weight: 600;
}

.message {
  font-size: 28rpx;
  color: #666;
  line-height: 1.6;
  /* margin-bottom: 40rpx; */
  display: block;
  text-indent: 2em;
  font-weight: 600;
}

.contact-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx;
}

.contact-text {
  font-size: 28rpx;
  color: #333;
}

.icon-wrapper {
  width: 64rpx;
  height: 64rpx;
  flex-shrink: 0;
  margin-right: 20rpx;
}

.icon {
  width: 100%;
  height: 100%;
}

.phone-numbers {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
  margin-left: 20rpx;
}

.phone-content {
  width: 100%;
  display: flex;
  flex-direction: column;
}

.phone-content > view:first-child {
  display: flex;
  align-items: center;
  margin-bottom: 15rpx;
}

.phone-action {
  align-self: flex-start;
  margin-left: 84rpx;
  padding: 8rpx 20rpx;
}

.qr-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  /* padding: 40rpx 0; */
  background-color: #fff;
  border-radius: 16rpx;
  width: 100%;
  position: relative;
  z-index: 1;
}

.qr-codes-container {
  display: flex;
  justify-content: space-around;
  /* flex-direction: column; */
  align-items: center;
  width: 100%;
  padding: 0 20rpx;
}

.qr-code-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 300rpx;
  padding: 20rpx;
}

.qr-code {
  width: 100%;
  height: 300rpx;
  background-color: #fff;
}

.qr-text {
  margin-top: 20rpx;
  font-size: 28rpx;
  color: #333;
  font-weight: 400;
  text-align: center;
  width: 100%;
}

.location-content {
  width: 100%;
  display: flex;
  flex-direction: column;
}

.location-content > view:first-child {
  display: flex;
  align-items: center;
  margin-bottom: 15rpx;
}

.location-action {
  align-self: flex-start;
  margin-left: 84rpx;
  padding: 8rpx 20rpx;
}

.card-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}
</style>

<style lang="scss" scoped>
.shuom-describe-container {
  padding: 30rpx;
}

.shuom-describe-card {
  position: relative;
  width: 100%;
  border-radius: 20rpx;
  overflow: hidden;
}

.shuom-describe-bg-image {
  width: 100%;
  height: auto;
  display: block;
}

.shuom-describe-content {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 40rpx;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding-bottom: 60rpx;
}

.shuom-describe-text-content {
  .shuom-describe-paragraph {
    display: block;
    color: #333;
    font-size: 28rpx;
    line-height: 1.5;
    margin-bottom: 20rpx;
    text-indent: 2em;
    font-weight: 600;

    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
